package com.zzvcom.doc.util;

import cn.hutool.core.collection.CollUtil;
import com.zzvcom.doc.WFixItem;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class Utils {

    /**
     * 不太成熟
     * 暂时先不使用，手动进行自适应表格的宽高
     * @param sourceList 表格的宽度列表
     * @param minSize 单元个最小的宽度
     * @param maxSize  所有的单元格的宽度。
     * @return
     */
    public static Map<BigInteger, WFixItem> fixW(List<BigInteger> sourceList, BigInteger minSize, BigInteger maxSize) {
        BigInteger totalSize = BigInteger.ZERO;
        for (BigInteger bigInteger : sourceList) {
            totalSize = totalSize.add(bigInteger);
        }
        BigDecimal sourceMax = BigDecimal.valueOf(totalSize.longValue());
        BigDecimal maxSizeB = BigDecimal.valueOf(maxSize.longValue());
        BigDecimal minSizeB = BigDecimal.valueOf(minSize.longValue());
        //计算百分比，首次分配
        Map<BigInteger, WFixItem> mapSize = new HashMap<>();
        //增加的数量
        BigDecimal fixTotalSize = BigDecimal.ZERO;
        List<WFixItem> nextList = new ArrayList<>();
        for (BigInteger bigInteger : sourceList) {
            BigDecimal resultSize = BigDecimal.valueOf(bigInteger.longValue()).multiply(maxSizeB).divide(
                    sourceMax, 0, RoundingMode.DOWN
            );
            WFixItem wFixItem = new WFixItem();
            wFixItem.setSource(bigInteger);
            wFixItem.setResult(resultSize);
            if (wFixItem.getResult().compareTo(minSizeB) < 0) {
                BigDecimal fixSize = minSizeB.subtract(wFixItem.getResult());
                wFixItem.setFixSize(fixSize);
                wFixItem.setResult(minSizeB);
                fixTotalSize = fixTotalSize.add(fixSize);
            } else {
                nextList.add(wFixItem);
            }
            mapSize.put(bigInteger, wFixItem);
        }
        processNextList(nextList, fixTotalSize, minSizeB);
        return mapSize;
    }

    private static void processNextList(List<WFixItem> nextList, BigDecimal fixTotalSize, BigDecimal minSizeB) {
        if (fixTotalSize.compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
        if (CollUtil.isEmpty(nextList)) {
            return;
        }
        BigDecimal nextTotal = BigDecimal.ZERO;
        for (WFixItem wFixItem : nextList) {
            nextTotal = nextTotal.add(wFixItem.getResult());
        }
        BigDecimal fixNextTotal = nextTotal.subtract(fixTotalSize);

        List<WFixItem> nList = new ArrayList<>();
        BigDecimal nfixTotalSize = BigDecimal.ZERO;
        for (WFixItem wFixItem : nextList) {
            BigDecimal result = wFixItem.getResult().multiply(fixNextTotal).
                    divide(nextTotal, 0, RoundingMode.DOWN);
            if (result.compareTo(minSizeB) < 0) {
                BigDecimal fixSize = minSizeB.subtract(result);
                wFixItem.setResult(minSizeB);
                wFixItem.setFixSize(fixSize);
                nfixTotalSize = nfixTotalSize.add(fixSize);
            } else {
                nList.add(wFixItem);
            }
        }
        // 查看余下的大小。
        processNextList(nList, nfixTotalSize, minSizeB);
    }
}
